/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/Renderer.h"

namespace alkes {

Renderer::Renderer(LockImageBufferWritable* locked_image)
: buffer_(*locked_image)
, pitch_(locked_image->getPitch())
, format_(locked_image->getPixelFormat())
, width_(locked_image->getImageWidth())
, height_(locked_image->getImageHeight())
{
    traits_ = alkes_get_pixel_format_traits(format_);
}

Renderer::Renderer(uint8_t* buffer, int32_t pitch, PixelFormat format, uint16_t width, uint16_t height)
: buffer_(buffer)
, pitch_(pitch)
, format_(format)
, width_(width)
, height_(height)
{
    traits_ = alkes_get_pixel_format_traits(format_);
}

Renderer::~Renderer()
{
}

const Color Renderer::getPixel(const Point2D& point) const
{
    uint8_t* p = getPointer(point);
    if (!p) return Color::TRANS;

    return traits_->getPixel(p);
}

uint8_t* Renderer::getScanline(uint16_t y) const
{
    if (height_ <= y)
        return 0;

    return buffer_ + pitch_ * y;
}

uint8_t* Renderer::getPointer(const Point2D& point) const
{
    if (point.x < 0 || width_ <= point.x)
        return 0;

    uint8_t* ptr = getScanline(point.y);

    return ptr ? ptr + traits_->bytes * point.x : 0;
}

void Renderer::setPixel(const Point2D& point, const Color& color)
{
    uint8_t* p = getPointer(point);
    if (!p) return ;

    traits_->setPixel(p, color);
}

void Renderer::drawLine(const Point2D& point, uint16_t width, const Color& color)
{
    uint8_t* p = getPointer(point);
    if (!p) return ;

    if (width_ <= point.x + width)
        width = width_ - point.x;

    traits_->fillScanline(p, width, color);
}

void Renderer::drawLine(const Point2D& /*point0*/, const Point2D& /*point1*/, const Color& /*color*/)
{
    LOGW("Not support: Renderer::drawLine()");
}

void Renderer::fill(const Color& color)
{
    uint8_t* ptr = getScanline(0);
    uint16_t i;
    for (i = 0; i < height_; ++i, ptr += pitch_)
    {
        traits_->fillScanline(ptr, width_, color);
    }
}

}
